package mesweb.mesweb.controller.produce;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import mesweb.mesweb.dto.ProducePlan.ProduceLinePlanResourceDto;
import mesweb.mesweb.dto.ProducePlan.ProduceLinePlanResourceSelDto;
import mesweb.mesweb.service.produce.IProduceLinePlanResourceService;
import mesweb.mesweb.vo.ProducePlan.ProduceLinePlanResourceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 产线工序资源配置
 * @Author: jeecg-boot
 * @Date:   2021-12-07
 * @Version: V1.0
 */
@Slf4j
@Api(tags="产线工序资源配置")
@RestController
@RequestMapping("/mes/produceLinePlanResource")
public class ProduceLinePlanResourceController {
	@Autowired
	private IProduceLinePlanResourceService produceLinePlanResourceService;


    @GetMapping("/GetAll")
    @ApiOperation("获取全部数据列表")
    public CommonResult<?> GetAll(@ApiParam("名称") @RequestParam( value = "produceLineId",required = false) Long produceLineId) {
        List<ProduceLinePlanResource> data = new ArrayList<>();
        if (produceLineId <=0) {
            data = produceLinePlanResourceService.list();
        } else {
            QueryWrapper<ProduceLinePlanResource> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("produceLineId",produceLineId);
            data = produceLinePlanResourceService.list(queryWrapper);
        }
        return CommonResult.success(data);
    }
	/**
	 * 分页列表查询
	 *
	 * @param produceLinePlanResourceSelDto
	 * @param page
	 * @param size
	 * @param req
	 * @return
	 */
	@ApiOperation(value="产线工序资源配置-分页列表查询", notes="产线工序资源配置-分页列表查询")
	@GetMapping(value = "/list")
	public CommonResult<?> queryPageList(ProduceLinePlanResourceSelDto produceLinePlanResourceSelDto,
								   @RequestParam(name="page", defaultValue="1") Integer page,
								   @RequestParam(name="size", defaultValue="10") Integer size,
								   HttpServletRequest req) {
		QueryWrapper<ProduceLinePlanResourceVo> queryWrapper = new QueryWrapper<>();
		if ( null != produceLinePlanResourceSelDto.getProduceLineId() && produceLinePlanResourceSelDto.getProduceLineId() > 0) {
			queryWrapper.eq("a.produce_line_id",produceLinePlanResourceSelDto.getProduceLineId());
		}
		if (null != produceLinePlanResourceSelDto.getProcedureId() &&  produceLinePlanResourceSelDto.getProcedureId() > 0) {
			queryWrapper.eq("a.procedure_id",produceLinePlanResourceSelDto.getProcedureId());
		}

		if (null != produceLinePlanResourceSelDto.getProduceYear() && produceLinePlanResourceSelDto.getProduceYear() > 0) {
			queryWrapper.eq("a.produce_year",produceLinePlanResourceSelDto.getProduceYear());
		}

		if (null != produceLinePlanResourceSelDto.getProduceMonth() && produceLinePlanResourceSelDto.getProduceMonth() > 0) {
			queryWrapper.eq("a.produce_month",produceLinePlanResourceSelDto.getProduceMonth());
		}

		Page<ProduceLinePlanResourceVo> pageProduceLinePlanResource = new Page<ProduceLinePlanResourceVo>(page, size);
		IPage<ProduceLinePlanResourceVo> pageList = produceLinePlanResourceService.getProduceLinePlanResources(pageProduceLinePlanResource, queryWrapper);
		return CommonResult.success(pageList);
	}
	/**
	 * 编辑
	 *
	 * @param produceLinePlanResource
	 * @return
	 */
	@ApiOperation(value="产线工序资源配置-编辑", notes="产线工序资源配置-编辑")
	@PostMapping(value = "/save")
	public CommonResult<?> edit(@RequestBody ProduceLinePlanResource produceLinePlanResource) {
       boolean result = false;
        if(produceLinePlanResource.getId() == 0L) {
             result = produceLinePlanResourceService.save(produceLinePlanResource);
        }
        else
        {
            result = produceLinePlanResourceService.updateById(produceLinePlanResource);
        }
        return CommonResult.success(result);
	}





	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="产线工序资源配置-通过id删除", notes="产线工序资源配置-通过id删除")
	@DeleteMapping(value = "/delete")
	public CommonResult<?> delete(@RequestParam(name="id",required=true) String id) {
		produceLinePlanResourceService.removeById(id);
		return CommonResult.success("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@ApiOperation(value="产线工序资源配置-批量删除", notes="产线工序资源配置-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public CommonResult<?> deleteBatch(@ApiParam("ProduceLinePlanResourceIDs") @RequestBody List<Long> ids) {
		this.produceLinePlanResourceService.removeByIds(ids);
		return CommonResult.success("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="产线工序资源配置-通过id查询", notes="产线工序资源配置-通过id查询")
	@GetMapping(value = "/queryById")
	public CommonResult<?> queryById(@RequestParam(name="id",required=true) String id) {
		ProduceLinePlanResource produceLinePlanResource = produceLinePlanResourceService.getById(id);
		return CommonResult.success(produceLinePlanResource);
	}


	/**
	 * 编辑
	 *
	 * @param produceLinePlanResource
	 * @return
	 */
	@ApiOperation(value="产线工序资源配置-初始化设备资源", notes="产线工序资源配置-初始化设备资源")
	@PostMapping(value = "/initDeviceResource")
	public CommonResult<?> InitDeviceResource(@RequestBody ProduceLinePlanResourceDto produceLinePlanResource) {
		boolean result = false;
		produceLinePlanResourceService.initDeviceResource(produceLinePlanResource);
		return CommonResult.success("初始化产线设备资源成功！");
	}


	/**
	 * 编辑
	 *
	 * @param produceLinePlanResource
	 * @return
	 */
	@ApiOperation(value="产线工序资源配置-批量更新", notes="产线工序资源配置-批量更新")
	@PostMapping(value = "/updateBatch")
	public CommonResult<?> saveBatch(@RequestBody List<ProduceLinePlanResource> produceLinePlanResource) {
		boolean result = produceLinePlanResourceService.updateBatchById(produceLinePlanResource);
		return CommonResult.success(result);
	}
}
